﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Linq;
namespace G.Reader.Controls
{
    public class SlideUpControl : ListBox
    {
        public SlideUpControl()
        {
            base.DefaultStyleKey = typeof(SlideUpControl);
            base.Visibility = System.Windows.Visibility.Collapsed;
        }

        public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(SlideUpControl), new PropertyMetadata(false, IsOpenPropertyChanged));

        public bool IsOpen
        {
            get
            {
                return (bool)this.GetValue(IsOpenProperty);
            }
            set
            {
                this.SetValue(IsOpenProperty, value);
            }
        }

        static void IsOpenPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SlideUpControl sortControl = d as SlideUpControl;
            if (sortControl.IsOpen)
            {
                sortControl.Visibility = Visibility.Visible;
                SlideTransition slideTransition = new SlideTransition();
                slideTransition.Mode = SlideTransitionMode.SlideUpFadeIn;
                ITransition transition = slideTransition.GetTransition(sortControl);

                transition.Completed += delegate { transition.Stop(); };
                transition.Begin();

                //打开动画
                SwivelTransition swivelTransition = new Microsoft.Phone.Controls.SwivelTransition();
                swivelTransition.Mode = SwivelTransitionMode.FullScreenIn;
                var items = sortControl.GetVisualDescendants<ListBoxItem>();
                foreach (var item in items)
                {
                    ITransition animation = swivelTransition.GetTransition(item);
                    animation.Begin();
                }
            }
            else
            {
                //SlideTransition slideTransition = new SlideTransition();
                //slideTransition.Mode = SlideTransitionMode.SlideDownFadeOut;
                //ITransition transition = slideTransition.GetTransition(sortControl);
                //transition.Completed += delegate { transition.Stop(); sortControl.Visibility = Visibility.Collapsed; };
                //transition.Begin();

                SwivelTransition swivelTransition = new Microsoft.Phone.Controls.SwivelTransition();
                swivelTransition.Mode = SwivelTransitionMode.FullScreenOut;
                var items = sortControl.GetVisualDescendants<ListBoxItem>();
                int animateFinished = 0;
                int itemscount = items.Count();
                foreach (var item in items)
                {
                    ITransition animation = swivelTransition.GetTransition(item);
                    animation.Completed += delegate
                    {
                        if (++animateFinished == itemscount)
                        {
                            if (!sortControl.IsOpen)
                                sortControl.Visibility = Visibility.Collapsed;
                        }
                    };
                    animation.Begin();
                }

            }
        }
    }
}
